package com.dreamtour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.entity.Agencies;
import com.dreamtour.service.IAgenciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/agencies")
public class AgenciesController {
    @Autowired
    private IAgenciesService agenciesService;

    @RequestMapping("/getByPage")
    public IPage<Agencies> getByPage(Integer pageNum, Integer pageSize) {
        IPage<Agencies> page = agenciesService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @RequestMapping("/getByName")
    public List<Agencies> getByName(String agencyName) {
        QueryWrapper<Agencies> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("agencyName", agencyName);
        List<Agencies> list = agenciesService.list(queryWrapper);
        return list;
    }

    @GetMapping("/filter")
    public ResponseEntity<Map<String, Object>> filterAgencies(
            @RequestParam(required = false) String qualifyStatus) {

        QueryWrapper<Agencies> queryWrapper = new QueryWrapper<>();

        // 添加验证状态筛选条件
        if (qualifyStatus != null && !qualifyStatus.isEmpty()) {
            // 假设qualifyStatus的值是"0", "1", "2"分别代表，已通过，待审核,未通过
            // 这里需要根据实际的数据库字段类型和值进行调整
            queryWrapper.eq("qualifyStatus", qualifyStatus);
        }

        // 查询符合条件的旅行社
        List<Agencies> filteredAgencies = agenciesService.list(queryWrapper);

        // 准备返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("agencies", filteredAgencies);

        return ResponseEntity.ok(response);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Agencies agencies) {
        boolean b = agenciesService.updateById(agencies);
        System.out.println("**********************************");
        return b;
    }

    @RequestMapping("/updateByUserId")
    public boolean updateByUserId(@RequestBody Agencies agencies){
        QueryWrapper<Agencies> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", agencies.getUserId());
        boolean b = agenciesService.update(agencies,queryWrapper);
        return b;
    }

    @RequestMapping("/getAgenciesCountByMonth")
    public ResponseEntity<List<Map<String, Object>>> getAgenciesCountByMonth() {
        List<Map<String, Object>> monthlyCounts = agenciesService.countAgenciesByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyCounts);
    }
}
